package dnl.util.ui.table;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/**
  * 
  * 
  * @author Daniel Or
  * @version 1.0
  */
public class TModel extends AbstractTableModel {

	private ColumnData[] columns;
	private ArrayList<RowData> rows = new ArrayList<RowData>();

	/**
	 * Constructor for TModel.
	 */
	public TModel(ColumnData[] columns) {
		this.columns = columns;
	}

	/**
	 * Constructor for TModel.
	 */
	public TModel(RowData[] rowData, ColumnData[] columns) {
		this.columns = columns;
		for (int i = 0; i < rowData.length; i++) {
			this.rows.add(rowData[i]);
		}
	}

	/**
	 * @param rowData
	 */
	public void addRow(RowData rowData) {
		this.rows.add(rowData);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnClass(int)
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return columns[columnIndex].dataType;
	}

	/**
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return columns.length;
	}

	/**
	 * @param column
	 * @return ColumnData
	 */
	public ColumnData getColumnData(int column) {
		if (column < 0 || column >= columns.length)
			return null;
		return columns[column];
	}

	/**
	 * @see javax.swing.table.TableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int column) {
		if (column < 0 || column >= columns.length)
			return null;
		return columns[column].title;
	}

	public RowData[] getData() {
		return rows.toArray(new RowData[] {
		});
	}

	/**
	 * @param row
	 * @return RowData
	 */
	public RowData getRow(int row) {
		if (row >= rows.size() || row < 0)
			return null;
		return rows.get(row);
	}

	/**
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		if(rows == null)
			return 0;
		return rows.size();
	}

	public String getTitle() {
		return "";
	}

	/**
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= rows.size())
			return null;
		RowData rw = rows.get(rowIndex);
		if (rw == null)
			return null;
		
		// it may be that the row wishes to show a certain text for the value
		// which may differ from toString()
		String text = rw.getTextAt(columnIndex);
		if(text != null)
		    return text;
		return rw.getObjectAt(columnIndex);
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
	 */
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		RowData rw = rows.get(rowIndex);
		if (rw == null)
			return;
		rw.setObjectAt(columnIndex, aValue);
		this.fireTableCellUpdated(rowIndex, columnIndex);
	}

	/**
	 * @param rowData
	 */
	public void setData(RowData[] rowData) {
		rows.clear();
		for (int i = 0; i < rowData.length; i++) {
			rows.add(rowData[i]);
		}
		this.fireTableDataChanged();
	}

	/**
	 * @param rowIndex
	 * @param rowData
	 */
	public void setRow(int rowIndex, RowData rowData) {
		this.rows.set(rowIndex, rowData);
	}

		
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return getRow(rowIndex).isEditable() || getRow(rowIndex).isEditable(columnIndex);
	}

}
